﻿Imports System.IO
Imports System.Net
Imports System.Reflection

Public Class Library

    Public IsPS2LibInstalled As Boolean = False
    Public IsPS3LibInstalled As Boolean = False
    Public IsPS4LibInstalled As Boolean = False
    Public IsPSPLibInstalled As Boolean = False
    Public IsPSVLibInstalled As Boolean = False
    Public IsToolsLibInstalled As Boolean = False
    Public installedlibrarys As Integer = 0
    Public AutoUpdating As Boolean = False

#Region "Load_Librarys"

    Private Sub Load_PS2_Lib()

        'Load external library and create new control
        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\ps2-lib.dll")
        Dim libtype As Type = libfile.GetType("ps2_lib.Main")
        Dim PS2Library As New Control

        PS2Library = Activator.CreateInstance(libtype)

        'Set properties
        PS2Library.Size = New Size(1043, 680)
        PS2Library.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        PS2Library.Name = "PS2Lib"

        'Finally add the control in the correct location
        Me.Controls.Add(PS2Library)
        PS2Library.Location = New Point(393, 0)

        'Resize the control when you open a library in maximized window
        If Me.WindowState = FormWindowState.Maximized Then
            PS2Library.Size = New Size(1527, 991)
        End If

    End Sub

    Private Sub Load_PS3_Lib()

        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\ps3-lib.dll")
        Dim libtype As Type = libfile.GetType("ps3_lib.Main")
        Dim PS3Library As New Control

        PS3Library = Activator.CreateInstance(libtype)

        PS3Library.Size = New Size(1043, 680)
        PS3Library.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        PS3Library.Name = "PS3Lib"

        Me.Controls.Add(PS3Library)

        PS3Library.Location = New Point(393, 0)

        If Me.WindowState = FormWindowState.Maximized Then
            PS3Library.Size = New Size(1527, 991)
        End If

    End Sub

    Private Sub Load_PS4_Lib()

        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\ps4-lib.dll")
        Dim libtype As Type = libfile.GetType("ps4_lib.Main")
        Dim PS4Library As New Control

        PS4Library = Activator.CreateInstance(libtype)

        PS4Library.Size = New Size(1043, 680)
        PS4Library.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        PS4Library.Name = "PS4Lib"

        Me.Controls.Add(PS4Library)

        PS4Library.Location = New Point(393, 0)

        If Me.WindowState = FormWindowState.Maximized Then
            PS4Library.Size = New Size(1527, 991)
        End If

    End Sub

    Private Sub Load_PSP_Lib()

        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\psp-lib.dll")
        Dim libtype As Type = libfile.GetType("psp_lib.Main")
        Dim PSPLibrary As New Control

        PSPLibrary = Activator.CreateInstance(libtype)

        PSPLibrary.Size = New Size(1043, 680)
        PSPLibrary.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        PSPLibrary.Name = "PSPLib"

        Me.Controls.Add(PSPLibrary)

        PSPLibrary.Location = New Point(393, 0)

        If Me.WindowState = FormWindowState.Maximized Then
            PSPLibrary.Size = New Size(1527, 991)
        End If

    End Sub

    Private Sub Load_PSV_Lib()

        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\psv-lib.dll")
        Dim libtype As Type = libfile.GetType("psv_lib.Main")
        Dim PSVLibrary As New Control

        PSVLibrary = Activator.CreateInstance(libtype)

        PSVLibrary.Size = New Size(1043, 680)
        PSVLibrary.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        PSVLibrary.Name = "PSVLib"

        Me.Controls.Add(PSVLibrary)

        PSVLibrary.Location = New Point(393, 0)

        If Me.WindowState = FormWindowState.Maximized Then
            PSVLibrary.Size = New Size(1527, 991)
        End If

    End Sub

    Private Sub Load_Tools_Lib()

        Dim libfile As Assembly = Assembly.LoadFile(My.Computer.FileSystem.CurrentDirectory + "\librarys\tools-lib.dll")
        Dim libtype As Type = libfile.GetType("tools_lib.Main")
        Dim ToolsLibrary As New Control

        ToolsLibrary = Activator.CreateInstance(libtype)

        ToolsLibrary.Size = New Size(1043, 680)
        ToolsLibrary.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Right
        ToolsLibrary.Name = "ToolsLib"

        Me.Controls.Add(ToolsLibrary)

        ToolsLibrary.Location = New Point(393, 0)
        PSLogo.Visible = False

        If Me.WindowState = FormWindowState.Maximized Then
            ToolsLibrary.Size = New Size(1527, 991)
        End If

    End Sub

#End Region

    Private Sub Update_Libs()

        'Don't update if there's nothing to update
        If Not installedlibrarys = 0 Then
            AutoUpdating = True
            LibraryManager.Show()
        End If

    End Sub

    Private Sub Set_Label_Parents()

        PS2Installed.Parent = PS2Box
        PS2Installed.AutoSize = True
        PS2Installed.Location = New Point(220, 50)

        PS3Installed.Parent = PS3Box
        PS3Installed.AutoSize = True
        PS3Installed.Location = New Point(220, 50)

        PS4Installed.Parent = PS4Box
        PS4Installed.AutoSize = True
        PS4Installed.Location = New Point(220, 50)

        PSPInstalled.Parent = PSPBox
        PSPInstalled.AutoSize = True
        PSPInstalled.Location = New Point(220, 50)

        PSVInstalled.Parent = PSVBox
        PSVInstalled.AutoSize = True
        PSVInstalled.Location = New Point(220, 50)

        ToolsInstalled.Parent = ToolsBox
        ToolsInstalled.AutoSize = True
        ToolsInstalled.Location = New Point(220, 50)

    End Sub

    Private Function CheckAddress(ByVal URL As String) As Boolean
        Try
            Dim request As WebRequest = WebRequest.Create(URL)
            Dim response As WebResponse = request.GetResponse()
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Public Sub Check_Librarys()

        'Set installedlibrarys to "0" to have no issues
        installedlibrarys = 0

        If File.Exists(".\librarys\ps2-lib.dll") Then
            PS2Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            PS2Installed.GlowColor = Color.Green
            PS2Installed.Location = New Point(245, 50)

            IsPS2LibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

        If File.Exists(".\librarys\ps3-lib.dll") Then
            PS3Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            PS3Installed.GlowColor = Color.Green
            PS3Installed.Location = New Point(245, 50)
            IsPS3LibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

        If File.Exists(".\librarys\ps4-lib.dll") Then
            PS4Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            PS4Installed.GlowColor = Color.Green
            PS4Installed.Location = New Point(245, 50)
            IsPS4LibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

        If File.Exists(".\librarys\psp-lib.dll") Then
            PSPInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            PSPInstalled.GlowColor = Color.Green
            PSPInstalled.Location = New Point(245, 50)
            IsPSPLibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

        If File.Exists(".\librarys\psv-lib.dll") Then
            PSVInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            PSVInstalled.GlowColor = Color.Green
            PSVInstalled.Location = New Point(245, 50)
            IsPSVLibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

        If File.Exists(".\librarys\tools-lib.dll") Then
            ToolsInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed")
            ToolsInstalled.GlowColor = Color.Green
            ToolsInstalled.Location = New Point(245, 50)
            IsToolsLibInstalled = True
            installedlibrarys = installedlibrarys + 1
        End If

    End Sub

    Public Sub Clear_Old_Lib()

        'Search for my custom control and remove it
        For Each ctrl As Control In Me.Controls
            If ctrl.Name = "PS2Lib" Or ctrl.Name = "PS3Lib" Or ctrl.Name = "PS4Lib" Or ctrl.Name = "PSPLib" Or ctrl.Name = "PSVLib" Or ctrl.Name = "ToolsLib" Then
                ctrl.Dispose()
                Me.Controls.Remove(ctrl)
            End If
        Next

    End Sub

    Private Sub Library_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Set the installed labels's parent
        Set_Label_Parents()

        'Read config if auto-update is enabled ("Yes" is default)
        If INIs.INI_ReadValueFromFile("PS Multi Tools", "AutoUpdate", "Yes", ".\conf.ini") = "Yes" Then
            Check_Librarys()
            Update_Libs()
        Else
            'Check the installed librarys only
            Check_Librarys()
        End If

        If Not INIs.INI_ReadValueFromFile("PS Multi Tools", "Language", "EN", ".\conf.ini") = "EN" Then
            INIs.ChangeLanguage()
        End If

    End Sub

    Private Sub ExitBox_Click(sender As Object, e As EventArgs) Handles ExitBox.Click
        Me.Close()
    End Sub

    Private Sub SettingsBox_Click(sender As Object, e As EventArgs) Handles SettingsBox.Click
        Settings.Show()
    End Sub

    Private Sub PS2Box_Click(sender As Object, e As EventArgs) Handles PS2Box.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If PS2Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_PS2_Lib()
        End If

    End Sub

    Private Sub PS3Box_Click(sender As Object, e As EventArgs) Handles PS3Box.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If PS3Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_PS3_Lib()
        End If

    End Sub

    Private Sub PS4Box_Click(sender As Object, e As EventArgs) Handles PS4Box.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If PS4Installed.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_PS4_Lib()
        End If

    End Sub

    Private Sub PSPBox_Click(sender As Object, e As EventArgs) Handles PSPBox.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If PSPInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_PSP_Lib()
        End If

    End Sub

    Private Sub PSVBox_Click(sender As Object, e As EventArgs) Handles PSVBox.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If PSVInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_PSV_Lib()
        End If

    End Sub

    Private Sub ToolsBox_Click(sender As Object, e As EventArgs) Handles ToolsBox.Click

        If MainTxt.Visible = True Then
            MainTxt.Visible = False
        End If

        If ToolsInstalled.Text = INIs.GetStringOfLang("PS Multi Tools", "Installed") Then
            Clear_Old_Lib()
            Load_Tools_Lib()
        End If

    End Sub

End Class